package main

import (
    "github.com/peterh/liner"
    "strings"
    "gitee.com/shuohe/xutils/xterm"
    "fmt"
)

func RunAsConsole(kspath string) {
    loadKeyset(kspath)

    ln := liner.NewLiner()
    ln.SetCtrlCAborts(true)
    defer ln.Close()

    handlers := make(map[string]func(int, []string) error)
    handlers["genkey"] = genkey
    handlers["getkey"] = getkey

    for {
        cmd, err := ln.Prompt("> ")
        if err != nil {
            break
        }
        ln.AppendHistory(cmd)

        if cmd == "quit" || cmd == "exit" {
            break
        }

        words := strings.Split(strings.TrimSpace(cmd), " ")
        handler, ok := handlers[words[0]]
        if !ok {
            xterm.Color(xterm.RED).Println("Error: Unknown command.\n").Reset()
            continue
        }

        if err = handler(RunMode_Console, words); err != nil {
            xterm.Color(xterm.RED).Printf("Error: %v\n\n", err).Reset()
            continue
        }

        fmt.Println()
    }
}
